-
Notifications
You must be signed in to change notification settings - Fork 14
chore: replace respx
mocks with pytest-httpserver
in tests
#454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I chose I haven't deleted |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is good, but I am curious about the impit and those test_specific_timeouts_...
tests
It's still just an idea. But I think mocking the ‘request’ method for ‘impit’ clients will be something like: from collections.abc import Iterator
import impit
import pytest
class EndOfTestError(Exception):
"""Custom exception that is raised after the relevant part of the code is executed to stop the test."""
@pytest.fixture
def patch_request(monkeypatch: pytest.MonkeyPatch) -> Iterator[list]:
captured_calls = []
def mock_request(*args, **kwargs):
captured_calls.append({
'args': args,
'kwargs': kwargs,
'timeout': kwargs.get('timeout')
})
raise EndOfTestError
monkeypatch.setattr('impit.Client.request', mock_request)
yield captured_calls
monkeypatch.undo()
def test_dynamic_timeout_sync_client(patch_request: None) -> None:
captured_calls = patch_request
client = impit.Client()
expected_timeouts = [1, 2, 4, 5]
for timeout in expected_timeouts:
with pytest.raises(EndOfTestError):
client.request('GET', url="http://example.com", timeout=timeout)
actual_timeouts = [call['timeout'] for call in captured_calls]
assert actual_timeouts == expected_timeouts I believe this approach is the most promising. |
respx
mocks withpytest-httpserver
in tests